package com.bt.dmdb.enums;

import java.util.List;
import java.util.Map;
//import org.apache.commons.collections.CollectionUtils;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
//import com.bt.dmdb.bean.Filter;
//import com.bt.dmdb.helpers.CriteriaBuilder;
import com.bt.dmdb.helpers.SqlBuilder;

/* Do not modify the existing logic for filter generations. If required add new enumerators for any other matches.. */
public enum Match {/*
    OR("or") {

        @Override
        public CriteriaBuilder addFilters(List<Map<String, String>> filters, CriteriaBuilder cb) {
            if (CollectionUtils.isNotEmpty(filters)) {
                Disjunction disjunction = Restrictions.disjunction();
                for (Map<String, String> filterMap : filters) {
                    Filter filter = new Filter(filterMap);
                    Constraint constraint = filter.getConstraint();
                    disjunction.add(constraint.getCriterion(filter.getProperty(), filter.getValue(), filter.isIgnoreCase()));
                }
                cb.getCriteria().add(disjunction);
            }
            return cb;
        }

        @Override
        public SqlBuilder addFilters(List<Map<String, String>> filters, SqlBuilder sqlBuilder) {
            String query = sqlBuilder.getQuery();
            if (CollectionUtils.isNotEmpty(filters)) {
                query = "select * from (" + query + ") where ";
                sqlBuilder.setWhere(true);
                for (Map<String, String> filterMap : filters) {
                    Filter filter = new Filter(filterMap, "SqlFilter");
                    Constraint constraint = filter.getConstraint();
                    String condition = constraint.getFilter(filter.getProperty(), filter.getValue().toString(), filter.isIgnoreCase());
                    query = query + condition + " or ";
                }
                sqlBuilder.setQuery(query.endsWith(" or ") ? query.substring(0, query.length() - 4) : query);
            }
            return sqlBuilder;
        }
    },
    AND("and") {

        @Override
        public CriteriaBuilder addFilters(List<Map<String, String>> filters, CriteriaBuilder cb) {
            if (CollectionUtils.isNotEmpty(filters)) {
                for (Map<String, String> filterMap : filters) {
                    Filter filter = new Filter(filterMap);
                    Constraint constraint = filter.getConstraint();
                    cb.getCriteria().add(constraint.getCriterion(filter.getProperty(), filter.getValue(), filter.isIgnoreCase()));
                }
            }
            return cb;
        }

        @Override
        public SqlBuilder addFilters(List<Map<String, String>> filters, SqlBuilder sqlBuilder) {
            String query = sqlBuilder.getQuery();
            if (CollectionUtils.isNotEmpty(filters)) {
                query = "select * from (" + query + ") where ";
                sqlBuilder.setWhere(true);
                for (Map<String, String> filterMap : filters) {
                    Filter filter = new Filter(filterMap, "SqlFilter");
                    Constraint constraint = filter.getConstraint();
                    String condition = constraint.getFilter(filter.getProperty(), filter.getValue().toString(), filter.isIgnoreCase());
                    query = query + condition + " and ";
                }
                sqlBuilder.setQuery(query.endsWith(" and ") ? query.substring(0, query.length() - 5) : query);
            }
            return sqlBuilder;
        }
    },
    NONE("none") {

        @Override
        public CriteriaBuilder addFilters(List<Map<String, String>> filters, CriteriaBuilder cb) {
            return cb;
        }

        @Override
        public SqlBuilder addFilters(List<Map<String, String>> filters, SqlBuilder sqlBuilder) {
            return sqlBuilder;
        }
    };

    public final String text;

    private Match(String text) {
        this.text = text;
    }

    public static Match getMatch(String text) {
        for (Match match : Match.values()) {
            if (match.text.equals(text)) { return match; }
        }
        return NONE;
    }

    public abstract CriteriaBuilder addFilters(List<Map<String, String>> filters, CriteriaBuilder cb);

    public abstract SqlBuilder addFilters(List<Map<String, String>> filters, SqlBuilder sqlBuilder);
*/}
